package ldh.im.fxbase.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;

public class BaseDao {

    public <T> T getBeanById(QueryRunner queryRunner, Connection connection, Class<T> beanClass, String tableName, Number id) throws SQLException {
        String sql = String.format("select * from %s where id = ?", tableName);
        T t = queryRunner.query(connection, sql, new BeanHandler<>(beanClass), id);
        return t;
    }

    public void updateBeanById(QueryRunner queryRunner, Connection connection, String sql, Object...params) throws SQLException {
        int n = queryRunner.execute(connection, sql, params);
        if (n != 1) {
            throw new RuntimeException("更新数据失败!!");
        }
    }
}
